home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_u_z / vlistsam.zip / VLRARE.C < prev    next >
C/C++ Source or Header  |  1992-09-27  |  7KB  |  259 lines

  1. #include "vlistint.h"
  2.  
  3. LONG VLBNcCreateHandler( HWND hwnd, LPCREATESTRUCT lpcreateStruct)
  4. {
  5.   PVLBOX pVLBox;
  6.  
  7.   //
  8.   // Allocate storage for the vlbox structure
  9.   //
  10.   pVLBox = (PVLBOX) LocalAlloc(LPTR, sizeof(VLBOX));
  11.  
  12.   if (!pVLBox)
  13.       // Error, no memory
  14.       return((long)NULL);
  15.  
  16.   SetWindowWord(hwnd, 0, (WPARAM)(WORD)pVLBox);
  17.  
  18.   pVLBox->styleSave = lpcreateStruct->style;
  19.  
  20.   //
  21.   // Make sure that ther are no scroll bar styles
  22.   //
  23.   SetWindowLong(hwnd, GWL_STYLE,
  24.                 (LPARAM)(DWORD)( pVLBox->styleSave &
  25.                 ~WS_VSCROLL & ~WS_HSCROLL));
  26.  
  27.  
  28.   return((LONG)(DWORD)(WORD)hwnd);
  29.  
  30. }
  31.  
  32.  
  33. LONG VLBCreateHandler( PVLBOX pVLBox, HWND hwnd, LPCREATESTRUCT lpcreateStruct)
  34. {
  35.   LONG           windowStyle = pVLBox->styleSave;
  36.   RECT           rc;
  37.   TEXTMETRIC     TextMetric;
  38.   HDC            hdc;
  39.  
  40.   //
  41.   // Initialize Variables
  42.   //
  43.   pVLBox->hwnd            = hwnd;
  44.   pVLBox->hwndParent      = lpcreateStruct->hwndParent;
  45.   pVLBox->nId             = (int) lpcreateStruct->hMenu;
  46.   pVLBox->hInstance       = lpcreateStruct->hInstance;
  47.   pVLBox->nvlbRedrawState = 1;
  48.   pVLBox->lNumLogicalRecs = -2L;
  49.   pVLBox->lSelItem        = -1L;
  50.   pVLBox->wFlags          = 0;
  51.  
  52.   //
  53.   // Check for USEDATAVALUES
  54.   //
  55.   if ( windowStyle & VLBS_USEDATAVALUES )
  56.       pVLBox->wFlags  |= USEDATAVALUES;
  57.   else
  58.       pVLBox->wFlags &= ~USEDATAVALUES;
  59.  
  60.   //
  61.   // Dertermine if this VLB is storing string
  62.   //
  63.   pVLBox->wFlags |= HASSTRINGS;
  64.   if ((windowStyle & VLBS_OWNERDRAWFIXED )
  65.      && (!(windowStyle & VLBS_HASSTRINGS)))
  66.      pVLBox->wFlags &= ~HASSTRINGS;
  67.  
  68.   //
  69.   // Get the font height and Number of lines
  70.   //
  71.   hdc = GetDC(hwnd);
  72.   GetTextMetrics(hdc, &TextMetric);
  73.   ReleaseDC(hwnd,hdc);
  74.   pVLBox->nchHeight = TextMetric.tmHeight;
  75.   GetClientRect(hwnd,&rc);
  76.   pVLBox->nLines = ((rc.bottom - rc.top) / pVLBox->nchHeight);
  77.  
  78.   //
  79.   // Remove borders and scroll bars, add keyboard input
  80.   //
  81.   windowStyle = windowStyle & ~WS_BORDER & ~WS_THICKFRAME;
  82.   windowStyle = windowStyle & ~WS_VSCROLL & ~WS_HSCROLL;
  83.  
  84.   //
  85.   // Remove regular list box we don't support
  86.   //
  87.   windowStyle = windowStyle & ~LBS_SORT;
  88.   windowStyle = windowStyle & ~LBS_MULTIPLESEL;
  89.   windowStyle = windowStyle & ~LBS_OWNERDRAWVARIABLE;
  90.   windowStyle = windowStyle & ~LBS_MULTICOLUMN;
  91.   windowStyle = windowStyle & ~VLBS_USEDATAVALUES;
  92.  
  93.   //
  94.   // Add List box styles we have to have
  95.   //
  96.   windowStyle = windowStyle | LBS_WANTKEYBOARDINPUT;
  97.   windowStyle = windowStyle | LBS_NOINTEGRALHEIGHT;
  98.   windowStyle = windowStyle | LBS_NOTIFY;
  99.  
  100.   //
  101.   // create the list box window
  102.   //
  103.   pVLBox->hwndList =
  104.     CreateWindowEx((DWORD)0L,
  105.                  (LPSTR)"LISTBOX",(LPSTR)NULL,
  106.                  windowStyle | WS_CHILD,
  107.                  0,
  108.                  0,
  109.                  rc.right,
  110.                  rc.bottom,
  111.                  pVLBox->hwnd,
  112.                  (HMENU)VLBLBOXID,
  113.                  pVLBox->hInstance,
  114.                  NULL);
  115.  
  116.   if (!pVLBox->hwndList)
  117.       return((LONG)-1L);
  118.  
  119.   if ( pVLBox->styleSave & VLBS_DISABLENOSCROLL ) {
  120.      ShowScrollBar(pVLBox->hwndList, SB_VERT, TRUE);
  121.      EnableScrollBar(pVLBox->hwndList, SB_VERT, ESB_DISABLE_BOTH);
  122.   }
  123.  
  124.  
  125.   //
  126.   // Subclass the list box
  127.   //
  128.   pVLBox->lpfnLBWndProc = (WNDPROC)SetWindowLong(pVLBox->hwndList, GWL_WNDPROC,
  129.                                         (LONG)(WNDPROC)LBSubclassProc);
  130.  
  131.   return((LONG)(DWORD)(WORD)hwnd);
  132. }
  133.  
  134. void VLBNcDestroyHandler(HWND hwnd,  PVLBOX pVLBox, WPARAM wParam, LPARAM lParam)
  135. {
  136.  
  137.   if (pVLBox)
  138.       LocalFree((HANDLE)pVLBox);
  139.  
  140.   //
  141.   // In case rogue messages float through after we have freed the pVLBox, set
  142.   // the handle in the window structure to FFFF and test for this value at the
  143.   // top of the WndProc
  144.   //
  145.   SetWindowWord(hwnd, 0, (WPARAM)-1);
  146.  
  147.   DefWindowProc(hwnd, WM_NCDESTROY, wParam, lParam);
  148. }
  149.  
  150.  
  151. void VLBSetFontHandler( PVLBOX pVLBox, HANDLE hFont, BOOL fRedraw)
  152. {
  153.   pVLBox->hFont = hFont;
  154.  
  155.   SendMessage(pVLBox->hwndList, WM_SETFONT, (WPARAM)hFont, (LPARAM)FALSE);
  156.  
  157.   VLBSizeHandler(pVLBox, 0);
  158.  
  159.   if (fRedraw)
  160.     {
  161.       InvalidateRect(pVLBox->hwnd, NULL, TRUE);
  162.     }
  163. }
  164.  
  165.  
  166. void VLBSizeHandler( PVLBOX pVLBox, int nItemHeight)
  167. //
  168. // Recalculates the sizes of the internal control in response to a
  169. // resizing of the Virtual List Box window.
  170. //
  171. {
  172.   HDC             hdc;
  173.   TEXTMETRIC      TextMetric;
  174.   RECT            rcWindow;
  175.   RECT            rcClient;
  176.   HANDLE          hOldFont;
  177.  
  178.   //
  179.   // Set the line height
  180.   //
  181.   if ( nItemHeight ) {
  182.     pVLBox->nchHeight = nItemHeight;
  183.   }
  184.   else if ((pVLBox->styleSave & VLBS_OWNERDRAWFIXED) ) {
  185.     pVLBox->nchHeight = (int) SendMessage(pVLBox->hwndList, LB_GETITEMHEIGHT, 0,0L);
  186.   }
  187.   else {
  188.     hdc = GetDC(pVLBox->hwndList);
  189.     if (pVLBox->hFont)
  190.        hOldFont = SelectObject(hdc, pVLBox->hFont);
  191.     GetTextMetrics(hdc, &TextMetric);
  192.     pVLBox->nchHeight = TextMetric.tmHeight;
  193.     if (pVLBox->hFont)
  194.        SelectObject(hdc, hOldFont);
  195.     ReleaseDC(pVLBox->hwndList,hdc);
  196.   }
  197.  
  198.   //
  199.   // Get the main windows client area
  200.   //
  201.   GetClientRect(pVLBox->hwnd,&rcClient);
  202.  
  203.   //
  204.   // If there is a Window and
  205.   // If the list box is integral height ...
  206.   //
  207.   if ( pVLBox->hwnd && !(pVLBox->styleSave & VLBS_NOINTEGRALHEIGHT) ) {
  208.       //
  209.       // Does it need adjusting ??????
  210.       //
  211.       if (rcClient.bottom % pVLBox->nchHeight) {
  212.           //
  213.           // Adjust
  214.           //
  215.              GetWindowRect(pVLBox->hwnd,&rcWindow);
  216.              SetWindowPos(pVLBox->hwnd, NULL, 0, 0,
  217.                  rcWindow.right - rcWindow.left,
  218.                  ((rcClient.bottom / pVLBox->nchHeight) * pVLBox->nchHeight)
  219.                      + ((rcWindow.bottom-rcWindow.top) - (rcClient.bottom)),
  220.                  SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
  221.       }
  222.   }
  223.  
  224.   //
  225.   // Now adjust the child list box to fill the new main window's
  226.   // client area.
  227.   //
  228.   if ( pVLBox->hwndList ) {
  229.       GetClientRect(pVLBox->hwnd,&rcClient);
  230.       SetWindowPos(pVLBox->hwndList, NULL, 0, 0,
  231.          rcClient.right+(GetSystemMetrics(SM_CXBORDER)*2),
  232.          rcClient.bottom+(GetSystemMetrics(SM_CXBORDER)*2),
  233.          SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
  234.   }
  235.  
  236.   //
  237.   // calculate the number of lines
  238.   //
  239.   pVLBox->nLines = rcClient.bottom / pVLBox->nchHeight;
  240.  
  241.   //
  242.   // If there is stuff already int the list box
  243.   // update the display ( more items or fewer items now )
  244.   //
  245.   if ( pVLBox->lNumLogicalRecs != -2L ) {
  246.       if ( pVLBox->lNumLogicalRecs <= pVLBox->nLines ) {
  247.          VLBFirstPage(pVLBox);
  248.       }
  249.       else if ( pVLBox->wFlags & USEDATAVALUES ) {
  250.           VLBFindPage(pVLBox, SendMessage(pVLBox->hwndList, LB_GETITEMDATA, 0, 0L), FALSE );
  251.       }
  252.       else {
  253.           VLBFindPage(pVLBox, pVLBox->lToplIndex, FALSE);
  254.       }
  255.  
  256.   }
  257.  
  258. }
  259.